package cn.xfdzx.android.apps.shop.activity.remake;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.MainActivity;
import cn.xfdzx.android.apps.shop.adapter.remake.OrderMyRemake_MarketAdapter;
import cn.xfdzx.android.apps.shop.adapter.remake.OrderMyRemake_RetailAdapter;
import cn.xfdzx.android.apps.shop.adapter.remake.ShopRemake_RetailAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.AfterServiceUpLoadMoreImgBean;
import cn.xfdzx.android.apps.shop.bean.AfterServiceUpLoadSingImgBean;
import cn.xfdzx.android.apps.shop.bean.DissatisfiedReasonBean;
import cn.xfdzx.android.apps.shop.bean.MyRemakeListMarketBean;
import cn.xfdzx.android.apps.shop.bean.MyRemakeListRetailBean;
import cn.xfdzx.android.apps.shop.bean.PeiSongBeen;
import cn.xfdzx.android.apps.shop.bean.ShopRemakeListMarketBean;
import cn.xfdzx.android.apps.shop.bean.ShopRemakeListRetailBean;
import cn.xfdzx.android.apps.shop.contract.OrderRemakeContract;
import cn.xfdzx.android.apps.shop.presenter.OrderRemakePresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.Utils;

/*零售店铺评价页面*/
public class ShopRemake_RetailActivity extends BaseMvpActivity<OrderRemakePresenter>implements OrderRemakeContract.View, View.OnClickListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;      //标题栏-左边按钮
    @BindView(R.id.recycle_remake)
    RecyclerView recycle_remake;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private ShopRemake_RetailAdapter retailAdapter;//零售店铺评价适配器

    private int page = 1;
    private int limit = Constants.LIMIT;
    private String storeId;
    private int search = 1;//分类搜索 1 => 全部 2 => 好评 3 => 差评 4 => 有图的评价
    private int content = 2;//是否只查看有内容的评价 1=>有内容的评价 2 => 全部
    private List<ShopRemakeListRetailBean.DataBean.ReplyBean> reply = new ArrayList<>();//零售店铺评价列表

    private TextView tv_allstar;//综合评星
    private TextView tv_remakecount;//评论数量
    private TextView tv_all;//筛选全部
    private TextView tv_new;//最新
    private TextView tv_remakegood;//好评
    private TextView tv_remakebad;//差评
    private TextView tv_haveimg;//有图
    private TextView tv_havecontent;//有内容评价
    private View emptyView;

    @Override
    public int getLayoutId() {
        return R.layout.activity_remake_shopretail;
    }

    @Override
    public void initView() {
        storeId = getIntent().getStringExtra("storeId");
        mPresenter = new OrderRemakePresenter();
        mPresenter.attachView(this);

        //请求列表
        getShopRemarketList();

        initRecycle();
    }

    private void initRecycle() {
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getShopRemarketList();

            }
        });
        recycle_remake.setLayoutManager(new LinearLayoutManager(this));
        View headView = LayoutInflater.from(this).inflate(R.layout.activity_remake_shopretail_head, recycle_remake, false);

        tv_allstar = headView.findViewById(R.id.tv_allstar);
        tv_remakecount = headView.findViewById(R.id.tv_remakecount);
        tv_all = headView.findViewById(R.id.tv_all);
        tv_new = headView.findViewById(R.id.tv_new);
        tv_remakegood = headView.findViewById(R.id.tv_remakegood);
        tv_remakebad = headView.findViewById(R.id.tv_remakebad);
        tv_haveimg = headView.findViewById(R.id.tv_haveimg);
        tv_havecontent = headView.findViewById(R.id.tv_havecontent);
        retailAdapter = new ShopRemake_RetailAdapter(reply);
        recycle_remake.setAdapter(retailAdapter);
        retailAdapter.addHeaderView(headView);

        emptyView = getLayoutInflater().inflate(R.layout.view_emptyview, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        retailAdapter.setHeaderAndEmpty(true);
        retailAdapter.setEmptyView(emptyView);

        tv_all.setOnClickListener(this);
        tv_remakegood.setOnClickListener(this);
        tv_remakebad.setOnClickListener(this);
        tv_haveimg.setOnClickListener(this);
        tv_havecontent.setOnClickListener(this);
    }

    private void getShopRemarketList() {
        page = 1;
        Map<String,Object> map = new HashMap<>();
        map.put("storeId",storeId);
        map.put("limit",limit);
        map.put("page",page);
        map.put("search",search);
        mPresenter.getShopRemakeRetail(true,true,map);
    }


    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onUploadMoreImgSuccess(AfterServiceUpLoadMoreImgBean bean) {

    }

    @Override
    public void onUploadSingImgSuccess(AfterServiceUpLoadSingImgBean bean) {

    }

    @Override
    public void onGetDissatisfiedSuccess(DissatisfiedReasonBean bean) {

    }

    @Override
    public void onGetRemakeRetailSuccess(DissatisfiedReasonBean bean) {

    }

    @Override
    public void getPeisongInfoSuccess(PeiSongBeen bean) {

    }

    @Override
    public void onGetRemakeMarketSuccess(DissatisfiedReasonBean bean) {

    }

    @Override
    public void onGetMyRemakeListRetailSuccess(MyRemakeListRetailBean bean) {
    }

    @Override
    public void onGetMyRemakeListMarketSuccess(MyRemakeListMarketBean bean) {
    }

    @Override
    public void onGetDeleteRemakeSuccess(MyRemakeListMarketBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            page = 1;
            if (MainActivity.TYPE_IS_RETAIL == 0){
                mPresenter.getMyRemakeListRetail(true,true,limit+"",page);
            }else{
                mPresenter.getMyRemakeListMarket(true,true,limit+"",page);
            }
        }else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();

        }
    }

    @Override
    public void onGetShopRemakeRetailSuccess(ShopRemakeListRetailBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (page == 1) {
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        Map<String,Object> map = new HashMap<>();
                        map.put("storeId",storeId);
                        map.put("limit",limit);
                        map.put("page",page);
                        map.put("search",search);
                        mPresenter.getShopRemakeRetail(false,true,map);
                    }
                });
            }

            tv_allstar.setText(bean.getData().getComprehensive());

            if (bean.getData().getCount()>9999){
                tv_remakecount.setText("9999+");
                tv_all.setText("全部(9999+)");
            }else {
                tv_remakecount.setText(bean.getData().getCount()+"");
                tv_all.setText("全部("+bean.getData().getCount()+")");
            }
            if (bean.getData().getFavorableCount()>9999){
                tv_remakegood.setText("好评(9999+)");
            }else {
                if (bean.getData().getFavorableCount()==0){
                    tv_remakegood.setText("好评");
                }else {
                    tv_remakegood.setText("好评("+bean.getData().getFavorableCount()+")");
                }
            }
            if (bean.getData().getNegative()>9999){
                tv_remakebad.setText("差评(9999+)");
            }else {
                if (bean.getData().getNegative()==0){
                    tv_remakebad.setText("差评");
                }else {
                    tv_remakebad.setText("差评("+bean.getData().getNegative()+")");
                }

            }
            if (bean.getData().getIsImg()>9999){
                tv_haveimg.setText("有图(9999+)");
            }else {
                if (bean.getData().getIsImg()==0){
                    tv_haveimg.setText("有图");
                }else {
                    tv_haveimg.setText("有图("+bean.getData().getIsImg()+")");
                }

            }

            if (bean.getData().getIsHaveContentCount()>9999){
                tv_havecontent.setText("有内容(9999+)");
            }else {
                if (bean.getData().getIsHaveContentCount()==0){
                    tv_havecontent.setText("有内容");
                }else {
                    tv_havecontent.setText("有内容("+bean.getData().getIsHaveContentCount()+")");
                }

            }

            if (page == 1){
                mRefreshLayout.setNoMoreData(false);
                reply.clear();
                if (EmptyUtil.isNotEmpty(bean.getData().getReply())){
                    reply = bean.getData().getReply();
                    retailAdapter.refreshDatas(reply);
                }else {
                    retailAdapter.clearDatas();

                }
            }else {
                if (EmptyUtil.isNotEmpty(bean.getData().getReply())){
                    //reply.addAll(bean.getData().getReply());
                    retailAdapter.addData(bean.getData().getReply());
                }
            }

            if (EmptyUtil.isEmpty(bean.getData().getReply())) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                page++;
            }

        }else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onGetShopRemakeMarketSuccess(ShopRemakeListMarketBean bean) {

    }


    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;

        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_all:    //所有
                if (Utils.isFastClick()){
                    search = 1;
                    clearState();
                    tv_all.setTextColor(getResources().getColor(R.color.color_FFFFFF));
                    tv_all.setBackgroundResource(R.drawable.dfb9129_bg_r3);
                }

                break;
            case R.id.tv_remakegood:    //好评
                if (Utils.isFastClick()){
                    search = 2;
                    clearState();
                    tv_remakegood.setTextColor(getResources().getColor(R.color.color_FFFFFF));
                    tv_remakegood.setBackgroundResource(R.drawable.dfb9129_bg_r3);
                }

                break;
            case R.id.tv_remakebad:    //差评
                if (Utils.isFastClick()){
                    search = 3;
                    clearState();
                    tv_remakebad.setTextColor(getResources().getColor(R.color.color_FFFFFF));
                    tv_remakebad.setBackgroundResource(R.drawable.dfb9129_bg_r3);
                }

                break;
            case R.id.tv_haveimg:    //有图
                if (Utils.isFastClick()){
                    search = 4;
                    clearState();
                    tv_haveimg.setTextColor(getResources().getColor(R.color.color_FFFFFF));
                    tv_haveimg.setBackgroundResource(R.drawable.dfb9129_bg_r3);
                }

                break;
            case R.id.tv_havecontent:    //有内容
                if (Utils.isFastClick()){
                    search = 5;
                    clearState();
                    tv_havecontent.setTextColor(getResources().getColor(R.color.color_FFFFFF));
                    tv_havecontent.setBackgroundResource(R.drawable.dfb9129_bg_r3);
                }

                break;

        }
    }

    private void clearState() {
        tv_all.setBackgroundResource(R.drawable.df2f2f2_r3);
        tv_remakegood.setBackgroundResource(R.drawable.df2f2f2_r3);
        tv_remakebad.setBackgroundResource(R.drawable.df2f2f2_r3);
        tv_haveimg.setBackgroundResource(R.drawable.df2f2f2_r3);
        tv_havecontent.setBackgroundResource(R.drawable.df2f2f2_r3);

        tv_all.setTextColor(getResources().getColor(R.color.color_A1A1A1));
        tv_remakegood.setTextColor(getResources().getColor(R.color.color_A1A1A1));
        tv_remakebad.setTextColor(getResources().getColor(R.color.color_A1A1A1));
        tv_haveimg.setTextColor(getResources().getColor(R.color.color_A1A1A1));
        tv_havecontent.setTextColor(getResources().getColor(R.color.color_A1A1A1));

        //请求列表
        getShopRemarketList();
    }
}
